import request from '@/utils/request'

/**
 * 商品基础模块API
 */
export default {
  // 基础接口
  base: {
    // 获取商品列表
    list: (params = {}) => request.get('/api/v1/products', { params }),

    // 获取商品详情
    detail: (id) => request.get(`/api/v1/products/${id}`),

    // 获取商家的商品列表
    getMerchantProducts: (merchantId, params = {}) => request.get(`/api/v1/products/merchant/${merchantId}`, { params }),

    // 增加商品浏览次数
    incrementView: (id) => request.put(`/api/v1/products/${id}/view`)
  },

  // 搜索相关接口
  search: {
    // 简单搜索商品
    simple: (params) => request.get('/api/v1/products/search/simple', {
      params: {
        keyword: params.keyword,
        pageNum: Math.max(1, params.pageNum || 1),
        pageSize: Math.max(1, Math.min(50, params.pageSize || 10))
      }
    }),

    // 高级搜索商品
    advanced: (data) => request.post('/api/v1/products/search/advanced', data),

    // 获取热门商品
    getHot: (params = {}) => request.get('/api/v1/products/search/hot', {
      params: {
        categoryId: params.categoryId,
        limit: Math.max(1, Math.min(50, params.limit || 10))
      }
    }),

    // 获取新品列表
    getNew: (params = {}) => request.get('/api/v1/products/search/new', {
      params: {
        categoryId: params.categoryId,
        limit: Math.max(1, Math.min(50, params.limit || 10))
      }
    }),

    // 获取搜索建议
    getSuggestions: (keyword, limit = 10) => request.get('/api/v1/products/search/suggestions', {
      params: {
        keyword,
        limit: Math.max(1, Math.min(50, limit))
      }
    }),

    // 获取搜索历史 [需要登录]
    getHistory: (limit = 10) => request.get('/api/v1/products/search/history', {
      params: {
        limit: Math.max(1, Math.min(50, limit))
      }
    }),

    // 清除搜索历史 [需要登录]
    clearHistory: () => request.delete('/api/v1/products/search/history'),

    // 记录搜索历史 [需要登录]
    recordHistory: (keyword) => request.post('/api/v1/products/search/history', null, {
      params: { keyword }
    })
  },

  // 商品分类相关接口
  category: {
    // 获取所有分类
    getAll: () => request.get('/api/v1/products/categories'),

    // 获取分类树结构
    getTree: () => request.get('/api/v1/products/categories/tree'),

    // 根据ID获取分类
    getById: (id) => request.get(`/api/v1/products/categories/${id}`),

    // 获取子分类
    getChildren: (parentId) => request.get(`/api/v1/products/categories/children/${parentId}`),

    // 分页获取分类
    getPage: (params = {}) => request.get('/api/v1/products/categories/page', {
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        name: params.name
      }
    }),

    // 获取分类列表
    getList: (parentId) => request.get('/api/v1/products/categories/list', {
      params: { parentId }
    }),

    // 获取次元分类
    getDimensionCategories: (dimension) => request.get('/api/v1/products/categories/dimension', {
      params: { dimension }
    }),

    // 获取分类商品列表
    getProducts: (categoryId, params = {}) => request.get(`/api/v1/products/categories/${categoryId}/products`, {
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10
      }
    }),

    // 获取多级分类筛选商品列表
    getFilteredProducts: (categoryIds, params = {}) => request.get('/api/v1/products/categories/products/filter', {
      params: {
        categoryIds: categoryIds,
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10
      }
    })
  },

  // 商品收藏相关接口
  favorite: {
    // 添加收藏
    add: (productId) => request.post(`/api/v1/products/favorites/${productId}`),

    // 取消收藏
    cancel: (productId) => request.delete(`/api/v1/products/favorites/${productId}`),

    // 获取用户收藏列表
    getUserFavorites: (params = {}) => request.get('/api/v1/products/favorites/users', {
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10
      }
    }),

    // 检查是否已收藏
    checkFavorite: (productId) => request.get(`/api/v1/products/favorites/products/${productId}/check`),

    // 获取商品收藏数量
    getFavoriteCount: (productId) => request.get(`/api/v1/products/favorites/products/${productId}/count`)
  },

  // 商品评论相关接口
  comment: {
    // 获取商品评论列表
    getProductComments: (productId, params = {}) => request.get(`/api/v1/products/comments/product/${productId}`, {
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10
      }
    }),

    // 检查商品是否已评价
    checkHasReviewed: (orderId, productId) => request.get('/api/v1/products/comments/check-reviewed', {
      params: {
        orderId,
        productId
      }
    })
  },

  // 商品推荐相关接口 [需要登录]
  recommendation: {
    // 获取个性化推荐商品
    getPersonalized: (limit = 10) => request.get('/api/v1/products/recommendations/personalized', {
      params: { limit }
    }),

    // 获取基于浏览历史的推荐商品
    getBrowseHistory: (limit = 10) => request.get('/api/v1/products/recommendations/browse-history', {
      params: { limit }
    }),

    // 获取基于收藏的推荐商品
    getFavorites: (limit = 10) => request.get('/api/v1/products/recommendations/favorites', {
      params: { limit }
    }),

    // 获取基于搜索历史的推荐商品
    getSearchHistory: (limit = 10) => request.get('/api/v1/products/recommendations/search-history', {
      params: { limit }
    })
  },
  // 商品规格相关接口
specs: {
  /**
   * 获取商品规格信息
   * @param {number} productId 商品ID
   * @returns {Promise<Array<ProductSpecVO>>} 规格信息列表
   * ProductSpecVO 结构示例:
   * {
   *   name: "颜色",
   *   values: ["红色", "蓝色", "黑色"]
   * }
   */
  getSpecs: (productId) => request.get(`/api/v1/products/${productId}/specs`),

  /**
   * 获取商品SKU列表
   * @param {number} productId 商品ID
   * @returns {Promise<Array<ProductSkuVO>>} SKU信息列表
   * ProductSkuVO 结构示例:
   * {
   *   id: 1,
   *   specs: { "颜色": "红色", "尺寸": "S" },
   *   price: 99.00,
   *   stock: 100
   * }
   */
  getSkus: (productId) => request.get(`/api/v1/products/${productId}/skus`)
}
} 